
const storeName = "demo_system"
const tokenKey = "token"
const userIdKey = "userId"
const autoKey = "auto"
const familyKey = "family"
const houseHoldKey = "houseHold"

/**
 * 持久化数据 需要实现get set clear三个函数才行
 */


const init = () => {
    const InitData = {
        userId: 0,
        token: ""
    }
    localStorage.setItem(storeName, JSON.stringify(InitData))
    // uni.setStorageSync(storeName, JSON.stringify(InitData))
}

export const get = () => {

    // if (uni.getStorageSync(storeName) === null) {
    //     init()
    // }

    if (!localStorage.getItem(storeName)) {
        init()
    }

    // return JSON.parse(uni.getStorageSync(storeName))
    return JSON.parse(localStorage.getItem(storeName))
}

export const set = (key, value) => {
    const store = get()
    store[key] = value
    // uni.setStorageSync(storeName, JSON.stringify(store))
    localStorage.setItem(storeName, JSON.stringify(store))
}

/**
 * 存储token
 * @param token token值
 */
export const setToken = (token) => {
    set(tokenKey, token)
}

/**
 * 获取token
 * @return String
 */
export const getToken = () => {
    return get()[tokenKey]
}

/**
 * 存储用户Id
 * @param userId 用户Id
 */
export const setUserId = (userId) => {
    set(userIdKey, userId)
}

/**
 * 获取用户Id
 * @return Integer
 */
export const getUserId = () => {
    return get()[userIdKey]
}

/**
 * 清除token
 */
export const clearToken = () => {
    set(tokenKey, "")
}

/**
 * 清除用户Id
 */
export const clearUserId = () => {
    set(userIdKey, "")
}

/**
 * 设置自动登录凭证
 * @param data
 */
export const setAuto = (data) => {
    set(autoKey, data)
}

/**
 * 获取自动登录凭证
 *
 */
export const getAuto = () => {
    return get()[autoKey]
}

/**
 * 清除自动登录凭证
 *
 */
export const clearAuto = () => {
    set(autoKey, "")
}

export const setFamily = (data) => {
    set(familyKey, data)
}

export const getFamily = () => {
    return get()[familyKey]
}

export const clearFamily = () => {
    set(familyKey, "")
}

export const setHouseHold = (data) => {
    set(houseHoldKey, data)
}
export const getHouseHold = () => {
    return get()[houseHoldKey]
}

export const clearHouseHold = () => {
    set(houseHoldKey, "")
}

/**
 * 清除所有
 */
export const clear = () => {
    uni.removeStorageSync(storeName)
}


